﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TPWB14
    {
      

        public TPWB14()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBEW();
            this.DataContext = MV;
            InitializeComponent();
        }
       
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            DataSet SResult;
            DataRow dwData;

            dwData = TBMain.NewRow();
            SResult = ((VWBBEW)MV).QueryWBBEW(dwData, -2);
            MergeData(SResult);
        }
        public override void New()
        {
            DataRow dwData;
            dwData = TBMain.NewRow();
            if (dwWB == null)
                return;
            string _time = "";
            string _date = "";
            Helpers.Helper.GetTime(ref _date, ref _time);
            switch (WBEWBS)
            {
                case "10":
                    dwData["WBEWMANDANT"] = dwWB["WBBMANDANT"];
                    dwData["WBEWJJJJ"] = MV.year;
                    WBEWBELEG = VSDDAT.SD22.GETNEXT("0022", "  ", "BEWEGUNG");  
                    dwData["WBEWBELEG"] = WBEWBELEG;
                    dwData["WBEWLFDNR"] = 1;
                    dwData["WBEWWAKZ"] = dwWB["WBBWAKZ"];
                    dwData["WBEWART"] = dwWB["WBBART"];
                    dwData["WBEWVAR"] = dwWB["WBBVAR"];
                    dwData["WBEWFARBE"] = dwWB["WBBFARBE"];
                    dwData["WBEWZENSUR"] = dwWB["WBBZENSUR"];
                    dwData["WBEWZENSURKZ"] = dwWB["WBBZENSURKZ"];
                    dwData["WBEWLO"] = dwWB["WBBLO"];
                    dwData["WBEWLB"] = dwWB["WBBLB"];
                    dwData["WBEWLP"] = dwWB["WBBLP"];

                    dwData["WBEWRWKRS"] = dwWB["WBBRWKRS"];
                    dwData["WBEWRWLFD"] = dwWB["WBBRWLFD"];
                    dwData["WBEWPOS"] = dwWB["WBBPOS"];
                    dwData["WBEWSTKNR"] = dwWB["WBBSTKNR"];
                    dwData["WBEWBS"] = WBEWBS;
                    dwData["WBEWWERTDAT"] = dwWB["WBBEINDAT"];
                    dwData["WBEWMTR"] = dwWB["WBBISTKLG"];
                    dwData["WBEWGEW"] = dwWB["WBBSGEW"];
                    dwData["WBEWBRT"] = dwWB["WBBSBRT"];

                    dwData["WBEWBUCHDAT"] = dwWB["WBBNEUDAT"];
                    dwData["WBEWBUCHUHR"] = _time;
                    dwData["WBEWBUCHBEN"] = dwWB["WBBNEUBEN"];
                    dwData["WBEWFC"] = "WB25";

                    break;
            }
            Insert(dwData);
        }
        private void Insert(DataRow _dwWB14)
        {
            dwData = ((VWBBEW)MV).Convert2WBBEW(_dwWB14, app.company, true);
            ((VWBBEW)MV).InsertWBBEW(dwData, app.company);
            SResult = ((VWBBEW)MV).QueryWBBEW(dwData, 6);
            TBMain.Merge(SResult.Tables[0]);
            vwMaster.View.MoveCurrentTo(dwData); 
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.NewCommand(sender, e);
        }
        private void WBBISTKLG_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            NewCommand(null, null);
        }
        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VWBBEW)MV).DeleteWBBEW(dwMaster, 1);
            string WBBSTKNR = dwMaster["WBBSTKNR"].ToString();
            vwMaster.View.MoveCurrentTo(-1);
            string filter = "WBBSTKNR='" + WBBSTKNR + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            DataRow dwData;
            dwData = TBMain.Rows.Add();
            dwData.ItemArray = dwMaster.ItemArray;
            vwMaster.View.MoveCurrentToLast();
            base.CopyCommand(sender, e);
        }

        public override void Save()
        {
            base.Save();
            dwData = ((VWBBEW)MV).Convert2WBBEW(dwWB14, app.company, true);
            ((VWBBEW)MV).SaveWBBEW(dwData, app.company); 

        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                Save();
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


    }
}
